package com.ruoyi.system.controller;

import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.api.domain.CountryTwoCode;
import com.ruoyi.system.domain.bo.CountryTwoCodeBo;
import com.ruoyi.system.domain.vo.CountryTwoCodeVo;
import com.ruoyi.system.service.ICountryTwoCodeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 国家二字码 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/countryTwoCode")
public class CountryTwoCodeController extends BaseController {

    @Resource
    private ICountryTwoCodeService iCountryTwoCodeService;
    
    @GetMapping("/getCountryByCode")
    public CountryTwoCode getCountryByCode(@RequestParam("code") String code) {
        if (StringUtils.isEmpty(code))
            return null;
        return iCountryTwoCodeService.getCountryByCode(code);
    }

    /**
     * 查询国家二字码列表
     */
    @GetMapping("/list")
    public TableDataInfo<CountryTwoCodeVo> list(CountryTwoCodeBo bo, PageQuery pageQuery) {
        return iCountryTwoCodeService.queryPageList(bo, pageQuery);
    }

//    /**
//     * 导出国家二字码列表
//     */
//    @Log(title = "国家二字码", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(CountryTwoCodeBo bo, HttpServletResponse response) {
//        List<CountryTwoCodeVo> list = iCountryTwoCodeService.queryList(bo);
//        ExcelUtil.exportExcel(list, "国家二字码", CountryTwoCodeVo.class, response);
//    }

    /**
     * 获取国家二字码详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<CountryTwoCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(iCountryTwoCodeService.queryById(id));
    }

    /**
     * 新增国家二字码
     */
    @Log(title = "国家二字码", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CountryTwoCodeBo bo) {
        return iCountryTwoCodeService.insertByBo(bo) ? R.ok() : R.fail();
    }

    /**
     * 修改国家二字码
     */
    @Log(title = "国家二字码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CountryTwoCodeBo bo) {
        return iCountryTwoCodeService.updateByBo(bo) ? R.ok() : R.fail();
    }

    /**
     * 删除国家二字码
     *
     * @param ids 主键串
     */
    @Log(title = "国家二字码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return iCountryTwoCodeService.deleteWithValidByIds(Arrays.asList(ids), true) ? R.ok() : R.fail();
    }

    /**
     * 根据国家中文名获取国家二字码
     * @param countryCn
     * @return
     */
    @GetMapping("/getByCountryCn/{countryCn}")
    public R<Object> getByCountryCn(@PathVariable String countryCn){
        CountryTwoCode countryTwoCode = iCountryTwoCodeService.getByCountryCn(countryCn);
        if (countryTwoCode != null){
            return R.ok(countryTwoCode);
        }
        return R.fail();
    }
}
